package functional.conception;

import java.util.function.Function;

/**
 * 函数式编程：高阶函数
 *
 * @author Liu Yicong
 * @date 2025/10/7
 */
public class HigherOrderFunctions {
	public static void main(String[] args) {
		// 函数可以赋值给变量
		Function<Integer, Integer> square = x -> x * x;
		System.out.println("square(5) = " + square.apply(5));

		// 函数可以作为参数（applyTwice）
		System.out.println("applyTwice(square, 3) = " + applyTwice(square, 3));
	}

	// 接收函数，作为参数的高阶函数
	static int applyTwice(Function<Integer, Integer> f, int x) {
		return f.apply(f.apply(x));
	}
}
